package general;

import events.EruptionVolcanique;
import events.EventSystem;
import geometry.PlanetVar;
import geometry.PointSI;
import graphique.C;

import java.util.ArrayList;



import systems.AtmoSystem;
import systems.FluidSystem;
import systems.PlantSystem;

public class Mechanisme
{
	public float	g[][][];
	public Case	cases[][][];
	
	public FluidSystem	fs;
	public PlantSystem	ps;
	public AtmoSystem	as;
	public EventSystem	es;
	
	public PlanetVar	pl;
	
	private Case nmax;
	public ArrayList<Case> colorCases = new ArrayList<Case>();
	
	int ni;
	int nj;
	int nm;
	public boolean	miseAJourTerrain = false;
	
	public Mechanisme(int n, int noise )
	{
		int puiss = 3;
		
		System.out.println("Taille s : " + (n+1) );
		
		init( n +1 , n +1 , noise );
	}
	
	public Mechanisme()
	{
		init( 129 , 257 , 2 );
	}
	
	public void avancement()
	{
		//fs.avancer();
		ps.avancer();
		fs.avancer();
		es.avancer();
		//fs.source( nmax , 0.05 , 1 );
	}

	public void init(int nbCh, int nbCw , int noise )
	{
		System.out.println( nbCh + " " + nbCw );
		g = new float[6][nbCh][nbCw];
		cases = new Case[6][nbCh-1][nbCw-1];
		
		Perlin.conversion( g , noise );
		Perlin.normaliser( g );
		
		initCases();

		fs = new FluidSystem( this );
		
		//Perlin.presenter( g[0] );
		
		System.out.println("Terrain Fini");
		
		
		pl = new PlanetVar();
		pl.construireCarte( nbCh-1 , this );
		
		ps = new PlantSystem( this );
		ps.creationForets( cases );
		ps.construireVege( cases );
		ps.construireVoisin();
		
		es = new EventSystem(this);
		
		EruptionVolcanique ev = new EruptionVolcanique(nmax, 0.05, 10 );
		ev.setDate(20, 40);
		
		es.histoire.add( ev );
		
		
		/*
		as = new AtmoSystem();
		as.creationNuage( 129 , 129 , 129 , 2 );
		as.developpementNuage( pl );
		System.out.println("Nb z de nuage " + as.lNuage.get(0).a.size() );
		*/
		
		
		//presenter();
	}

	/*
	public static void main( String arg[] )
	{
		Mechanisme m = new Mechanisme();
		
		m.init( 201 , 201 );
		
		m.presenter();
	}
	*/

	private void initCases()
	{
		for( int m = 0 ; m < 6 ; m++ )
		{
			for( int i = 0 ; i < cases[0].length ; i++ )
			{
				for( int j = 0 ; j < cases[0][0].length ; j++ )
				{
					cases[m][i][j] = new Case(m, i, j);
				}
			}
		}
		
		nmax = null;
		float vmax = -1;
		
		for( int m = 0 ; m < 6 ; m++ )
		{
			for( int i = 0 ; i < cases[0].length ; i++ )
			{
				for( int j = 0 ; j < cases[0][0].length ; j++ )
				{
					for( int l = -1 ; l < 2 ; l++ )
					{
						for( int k = -1 ; k < 2 ; k++ )
						{
							ni = i+l;
							nj = j+k;
							nm = m;
							
							transformerBordure();
							
							if( ni >= 0 && ni < cases[0].length && nj >= 0 && nj < cases[0][0].length )
							{
								//if( g[m][i+l][j+k] < g[m][i][j] )
								{
									cases[m][i][j].asc.add( cases[nm][ni][nj] );
								}
							}
						}
					}
					
					if( g[m][i][j] > vmax )
					{
						vmax = g[m][i][j];
						nmax = cases[m][i][j];
					}
				}
			}
		}
		
		System.out.println("Max : " + vmax);
		
	}

	private void transformerBordure()
	{
		// 1 - 0
		if( nm == 0 && ni == cases[0].length )
		{
			nm = 1;
			ni = cases[0].length-1;
		}
		else if( nm == 1 && ni == cases[0].length )
		{
			nm = 0;
			ni = cases[0].length-1;
		}
		
		// 2 - 0
		if( nm == 0 && nj == cases[0].length )
		{
			nm = 2;
			int temp = ni;
			ni = cases[0].length-1;
			nj = temp;
		}
		else if( nm == 2 && ni == cases[0].length )
		{
			nm = 0;
			int temp = nj;
			nj = cases[0].length-1;
			ni = temp;
		}
		
		// 1 - 2
		if( nm == 0 && nj == cases[0].length )
		{
			nm = 3;
			nj = cases[0].length-1;
		}
		else if( nm == 3 && nj == cases[0].length )
		{
			nm = 0;
			nj = cases[0].length-1;
		}
		
		// 4 - 0
		if( nm == 0 && ni == -1 )
		{
			nm = 4;
			ni = cases[0].length-1;
		}
		else if( nm == 4 && ni == cases[0].length )
		{
			nm = 0;
			ni = 0;
		}
		
		// 3 - 1
		if( nm == 1 && ni == -1 )
		{
			nm = 3;
			ni = cases[0].length-1;
		}
		else if( nm == 3 && ni == cases[0].length )
		{
			nm = 1;
			ni = 0;
		}
		
		// 4 - 2
		if( nm == 2 && nj == -1 )
		{
			nm = 4;
			nj = cases[0].length-1;
		}
		else if( nm == 4 && nj == cases[0].length )
		{
			nm = 2;
			nj = 0;
		}
		
		// 5 - 1
		if( nm == 1 && nj == -1 )
		{
			nm = 5;
			nj = cases[0].length-1;
		}
		else if( nm == 5 && nj == cases[0].length )
		{
			nm = 1;
			nj = 0;
		}
		
		// 5 - 0
		if( nm == 0 && nj == -1 )
		{
			nm = 5;
			int temp = ni;
			ni = cases[0].length-1;
			nj = temp;
		}
		else if( nm == 5 && ni == cases[0].length )
		{
			nm = 0;
			int temp = nj;
			nj = 0;
			ni = temp;
		}
		
		// 5 - 4
		if( nm == 4 && nj == -1 )
		{
			nm = 5;
			nj = 0;
		}
		else if( nm == 5 && nj == -1 )
		{
			nm = 4;
			nj = 0;
		}
		
		// 3 - 5
		if( nm == 5 && ni == -1 )
		{
			nm = 3;
			int temp = nj;
			nj = 0;
			ni = temp;
		}
		else if( nm == 3 && nj == -1 )
		{
			nm = 5;
			int temp = ni;
			ni = 0;
			nj = temp;
		}
		
		// 3 - 4
		if( nm == 4 && ni == -1 )
		{
			nm = 3;
			ni = 0;
		}
		else if( nm == 3 && ni == -1 )
		{
			nm = 4;
			ni = 0;
		}
		
		// 3 - 2
		if( nm == 2 && ni == -1 )
		{
			nm = 3;
			int temp = nj;
			nj = cases[0].length-1;
			ni = temp;
		}
		else if( nm == 3 && nj == cases[0].length )
		{
			nm = 2;
			int temp = ni;
			ni = 0;
			nj = temp;
		}
	}

	public void presenter()
	{
		for( int i = 0 ; i < g.length ; i++ )
		{
			for( int j = 0 ; j < g[0].length ; j++ )
			{
				System.out.print( g[i][j] + " ");
			}
			System.out.println();
		}
	}
	
	public ArrayList<Case> grouping(ArrayList<Case> base )
	{
		ArrayList<Case> complet = new ArrayList<Case>();
		
		for( int i = 0 ; i < base.size() ; i++ )
		{
			for( int l = -1 ; l < 2 ; l++ )
			{
				for( int k = -1 ; k < 2 ; k++ )
				{
					ni = base.get(i).x+l;
					nj = base.get(i).y+k;
					nm = base.get(i).l;
					
					transformerBordure();
					
					if( ni >= 0 && ni < cases[0].length && nj >= 0 && nj < cases[0][0].length )
					{
						if( !complet.contains( cases[nm][ni][nj] ) )
						{
							complet.add( cases[nm][ni][nj] );
						}
					}
				}
			}
		}
		
		return complet;
	}

	public ArrayList<Case> colorGrouping(ArrayList<Case> base)
	{
		ArrayList<Case> complet = new ArrayList<Case>();
		
		for( int i = 0 ; i < base.size() ; i++ )
		{
			double fact = 0.1;
			
			PointSI p1m = base.get(i).zone.p1.mult( Math.random() * fact - fact/2 );
			PointSI p2m = base.get(i).zone.p2.mult( Math.random() * fact - fact/2 );
			PointSI p3m = base.get(i).zone.p3.mult( Math.random() * fact - fact/2 );
			PointSI p4m = base.get(i).zone.p4.mult( Math.random() * fact - fact/2 );

			base.get(i).zone.p1 = p1m;
			base.get(i).zone.p2 = p2m;
			base.get(i).zone.p3 = p3m;
			base.get(i).zone.p4 = p4m;
			
			for( int l = -1 ; l < 2 ; l++ )
			{
				for( int k = -1 ; k < 2 ; k++ )
				{
					ni = base.get(i).x+l;
					nj = base.get(i).y+k;
					nm = base.get(i).l;
					
					transformerBordure();
					
					if( ni >= 0 && ni < cases[0].length && nj >= 0 && nj < cases[0][0].length )
					{
						if( !complet.contains( cases[nm][ni][nj] ) )
						{
							complet.add( cases[nm][ni][nj] );
						}
						
						if( l == -1 && k == - 1)
						{
							cases[nm][ni][nj].zone.p1 = p4m;
						}
						
						
					}
				}
			}
		}
		
		return complet;
	}
}
